package com.deparse.dpframe.utils;

import android.content.Context;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.io.File;

/**
 * Created by hsy on 2016/2/25.
 */
public class DPPicassoUtil {
    private final Context context;
    private static int mErrorImageRes = -1;

    private DPPicassoUtil(Context context) {
        this.context = context;
    }

    public static DPPicassoUtil with(Context context) {
        return new DPPicassoUtil(context);
    }

    public <T> RequestCreator load(T path) {
        RequestCreator creator = null;
        if (path instanceof String) {
            creator = Picasso.with(context).load((String) path);
        } else if (path instanceof File) {
            creator = Picasso.with(context).load((File) path);
        } else if (path instanceof Integer) {
            creator = Picasso.with(context).load((Integer) path);
        }

        if (0 < getErrorImageRes() && null != creator) {
            creator = creator.error(getErrorImageRes());
        }

        return creator;
    }

    public static int getErrorImageRes() {
        return DPPicassoUtil.mErrorImageRes;
    }

    public static void setErrorImageRes(int mErrorImageRes) {
        DPPicassoUtil.mErrorImageRes = mErrorImageRes;
    }

    public static <T> void load(Context context, T src, ImageView imageView) {
        if (null == src) {
            imageView.setImageResource(mErrorImageRes);
            return;
        }
        DPPicassoUtil.with(context).load(src).into(imageView);
    }
}
